import importlib
from pathlib import Path
from app.extensions import db

def load_models():
    """自动加载models目录下所有继承db.Model的类"""
    models = {}
    for model_file in Path(__file__).parent.glob('*.py'):
        if model_file.name not in ('__init__.py', '__pycache__'):
            module_name = f'app.models.{model_file.stem}'
            module = importlib.import_module(module_name)
            for attr_name in dir(module):
                obj = getattr(module, attr_name)
                if isinstance(obj, type) and issubclass(obj, db.Model) and obj != db.Model:
                    models[attr_name] = obj
                    globals()[attr_name] = obj  # 注入当前命名空间
    return models
# 执行自动加载
_models = load_models()
__all__ = list(_models.keys())  # 设置__all__
print(f"Loaded models: {', '.join(__all__)}")